//
//  HelpViewController.m
//  舟航007机车卫士
//
//  Created by admin on 15/11/9.
//  Copyright (c) 2015年 redoor. All rights reserved.
//

#import "HelpViewController.h"
#import "Self-help110.h"
#import "JXBAdPageView.h"
#import "Masonry.h"
#import "LostCarAlarmViewController.h"
#import "AlarmRecordViewController.h"
#import "ServiceEvaluationViewController.h"
#import "AlertView.h"
#import "LoginViewController.h"

@interface HelpViewController ()
{
    NSString *eportStatus;
    NSString *psbtext;
    NSString *time;

}
@property (nonatomic,copy) NSDictionary * bicycleInfoDict;
@property (nonatomic,strong) JXBAdPageView * adView;//宣传片视图
@property (nonatomic, strong) NSString *userOrTourist;
@end

@implementation HelpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.userOrTourist = [[NSUserDefaults standardUserDefaults] objectForKey:@"userOrTourist"];
    
    [self.leftbtn removeFromSuperview];
    [self.rightbtn removeFromSuperview];
    [self.centerbtn setImage:nil forState:UIControlStateNormal];
    [self.centerbtn setTitle:@"自助110" forState:UIControlStateNormal];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self addJXBAdPageView];
    [self addButtonsView];
    
    /**
     *  查询我的车辆，并判断“查询备案”是否可用
     */
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(LostCarSuccessNotification:) name:@"LostCarSuccessNotification" object:nil];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden=YES;
   
    if ([self.userOrTourist isEqualToString:@"1"]) {
        [self getEbicycleInfo];
    }
     NSDictionary *dic=@{@"key":@"2"};
    [[NSNotificationCenter defaultCenter]postNotificationName:@"changetabbartwo" object:nil userInfo:dic];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - LostCarSuccessNotification

- (void)LostCarSuccessNotification:(NSNotification *) _notification
{
    AlarmRecordViewController * alarmRecordVC = [[AlarmRecordViewController alloc]init];
    alarmRecordVC.time=time;
    alarmRecordVC.eportStatus=eportStatus;
    alarmRecordVC.psbtext=psbtext;
    [self.navigationController pushViewController:alarmRecordVC animated:YES];
}

/**
 *  界面按钮处理函数
 *
 *  @param _btn 9个按钮
 */
- (void)buttonClickedHandle:(UIButton *) _btn
{
    if ([self.userOrTourist isEqualToString:@"1"]) {
        ButtonType type = _btn.tag;
        
        if ( AlarmRecord == type )
        {
            AlarmRecordViewController * alarmRecordVC = [[AlarmRecordViewController alloc]init];
            alarmRecordVC.time=time;
            alarmRecordVC.eportStatus=eportStatus;
            alarmRecordVC.psbtext=psbtext;
            [self.navigationController pushViewController:alarmRecordVC animated:YES];
        }
        else if ( LostCarAlarm == type )
        {
            LostCarAlarmViewController * lostCarAlarmVC = [[LostCarAlarmViewController alloc]init];
            [self.navigationController pushViewController:lostCarAlarmVC animated:YES];
        }
        else if ( ServiceEvaluation == type )
        {
            ServiceEvaluationViewController * serviceEvaluationVC = [[ServiceEvaluationViewController alloc]init];
            [self.navigationController pushViewController:serviceEvaluationVC animated:YES];
        }
    }
    else {
        AlertView *alertView = [[AlertView alloc] initWithConfirmButtonBlock:^{
            NSLog(@"登录");
            LoginViewController *loginVC = [[LoginViewController alloc]init];
            [self presentViewController:loginVC animated:YES completion:nil];
        } cancelButtonBlock:^{
            NSLog(@"取消");
        }];
        [self.view addSubview:alertView];

    }
}


#pragma mark - Customer method -

/**
 *  宣传广告图片
 */
- (void)addJXBAdPageView
{
    self.adView = [[JXBAdPageView alloc]initWithFrame:CGRectMake(0, CGRectGetMidY(self.navbackview.frame), SCREEN_WIDTH, SCREEN_HEIGHT*0.33)];
    self.adView.iDisplayTime = JXBADPAGEVIEW_DISPLAY_TIME;
    [self.adView startAdsWithBlock:@[@"changeone.jpg",@"changetwo.jpg",@"changethree.jpg",@"changefour.jpg"] block:^(NSInteger clickIndex) {
        NSLog(@"%ld",(long)clickIndex);
    }];
    [self.view addSubview:self.adView];
}

/**
 *  添加按钮视图
 */
- (void)addButtonsView
{
    int row = 0;
    int col = 0;
    ButtonType type = 0;
    CGFloat XIntVal = 0;
    CGFloat YIntVal = 0;
    CGFloat Ycoord  = 0;
    CGFloat Xcoord  = 0;

    UIButton * button = nil;
    
    Ycoord = CGRectGetMaxY(self.adView.frame)+15;
    XIntVal = (SCREEN_WIDTH - 5*BUTTON_SIZE_WIDTH)/2.0;
    YIntVal = (SCREEN_HEIGHT - Ycoord - CGRectGetHeight(self.tabBarController.tabBar.frame) - 4*BUTTON_SIZE_HEIGTH)/2.0;
    
    for ( row = 0; row < 3 ; row++ )
    {
        if ( 0 == row )
        {
            Ycoord += YIntVal+BUTTON_SIZE_HEIGTH/2.0;
        }
        else
        {
            Ycoord += YIntVal + BUTTON_SIZE_HEIGTH;
        }
        
        Xcoord = 0;
        for ( col = 0; col < 3 ; col++ )
        {
            if ( 0 == col )
            {
                Xcoord = BUTTON_SIZE_WIDTH;
            }
            else
            {
                Xcoord += BUTTON_SIZE_WIDTH + XIntVal;
            }

            type = AlarmRecord + (ButtonType)(3*row + col);
            if (type == Add)
            {
                button = [[UIButton alloc]initWithFrame:CGRectMake(Xcoord, Ycoord, BUTTON_SIZE_WIDTH, BUTTON_SIZE_WIDTH)];
            }
            else
            {
                button = [[UIButton alloc]initWithFrame:CGRectMake(Xcoord, Ycoord, BUTTON_SIZE_WIDTH, BUTTON_SIZE_HEIGTH)];
            }
            button.tag = type;
            
            if ( LostCarAlarm == type || ServiceEvaluation == type )
            {
                button.userInteractionEnabled = YES;
                [button setImage:[UIImage imageNamed:[self getButtonImageByButtonType:type]] forState:UIControlStateNormal];
            }
            else
            {
                if ([self.userOrTourist isEqualToString:@"1"]) {
                    button.userInteractionEnabled = NO;
                }
                else {
                    button.userInteractionEnabled = YES;
                }
                [button setImage:[UIImage imageNamed:[self getButtonOffImageByButtonType:type]] forState:UIControlStateNormal];
            }
            [button setImage:[UIImage imageNamed:[self getButtonOnImageByButtonType:type]] forState:UIControlStateHighlighted];
            [button addTarget:self action:@selector(buttonClickedHandle:) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:button];
        }
    }
}

/**
 *  根据按钮类型返回按钮上图片的名称
 * 注: 正常状态
 *
 *  @param _btnType 按钮类型
        AlarmRecord              = 0,//报警备案
        LostCarAlarm             = 1,//丢车报警
        ServiceEvaluation        = 2,//服务评价
        UnlockingService         = 3,//开锁服务
        LostAndFound             = 4,//失物招领
        OneKeyMove               = 5,//一键移车
        ResidencePermit          = 6,//居住证办理
        CensusRegisterManagement = 7,//户籍办理
        Add                      = 8 //添加
 *
 *  @return 按钮上图片的名称
 */
- (NSString *)getButtonImageByButtonType:( ButtonType ) _btnType
{
    NSString * imageName = nil;
    
    switch (_btnType)
    {
        case AlarmRecord:
            imageName = @"1pic_btn01";
            break;
            
        case LostCarAlarm:
            imageName = @"1pic_btn02";
            break;
            
        case ServiceEvaluation:
            imageName = @"1pic_btn03";
            break;
            
        case UnlockingService:
            imageName = @"1pic_btn04";
            break;
            
        case LostAndFound:
            imageName = @"1pic_btn05";
            break;
            
        case OneKeyMove:
            imageName = @"1pic_btn06";
            break;
            
        case ResidencePermit:
            imageName = @"1pic_btn07";
            break;
            
        case CensusRegisterManagement:
            imageName = @"1pic_btn08";
            break;
            
        case Add:
            imageName = @"1pic_btn_add";
            break;
            
        default:
            break;
    }
    return imageName;
}

/**
 *  根据按钮类型返回按钮上图片的名称
 * 注: off状态(置灰)
 *
 *  @param _btnType 按钮类型
 AlarmRecord              = 0,//报警备案
 LostCarAlarm             = 1,//丢车报警
 ServiceEvaluation        = 2,//服务评价
 UnlockingService         = 3,//开锁服务
 LostAndFound             = 4,//失物招领
 OneKeyMove               = 5,//一键移车
 ResidencePermit          = 6,//居住证办理
 CensusRegisterManagement = 7,//户籍办理
 Add                      = 8 //添加
 *
 *  @return 按钮上图片的名称
 */
- (NSString *)getButtonOffImageByButtonType:( ButtonType ) _btnType
{
    NSString * imageName = nil;
    
    switch (_btnType)
    {
        case AlarmRecord:
            imageName = @"1pic_btn01_off";
            break;
            
        case LostCarAlarm:
            imageName = @"1pic_btn02_off";
            break;
            
        case ServiceEvaluation:
            imageName = @"1pic_btn03_off";
            break;
            
        case UnlockingService:
            imageName = @"1pic_btn04_off";
            break;
            
        case LostAndFound:
            imageName = @"1pic_btn05_off";
            break;
            
        case OneKeyMove:
            imageName = @"1pic_btn06_off";
            break;
            
        case ResidencePermit:
            imageName = @"1pic_btn07_off";
            break;
            
        case CensusRegisterManagement:
            imageName = @"1pic_btn08_off";
            break;
            
        case Add:
            imageName = @"1pic_btn_add";
            break;
            
        default:
            break;
    }
    return imageName;
}


/**
 *  根据按钮类型返回按钮上图片的名称
 * 注: 点中状态
 *
 *  @param _btnType 按钮类型
 AlarmRecord              = 0,//报警备案
 LostCarAlarm             = 1,//丢车报警
 ServiceEvaluation        = 2,//服务评价
 UnlockingService         = 3,//开锁服务
 LostAndFound             = 4,//失物招领
 OneKeyMove               = 5,//一键移车
 ResidencePermit          = 6,//居住证办理
 CensusRegisterManagement = 7,//户籍办理
 Add                      = 8 //添加
 *
 *  @return 按钮上图片的名称
 */
- (NSString *)getButtonOnImageByButtonType:( ButtonType ) _btnType
{
    NSString * imageName = nil;
    
    switch (_btnType)
    {
        case AlarmRecord:
            imageName = @"1pic_btn01_on";
            break;
            
        case LostCarAlarm:
            imageName = @"1pic_btn02_on";
            break;
            
        case ServiceEvaluation:
            imageName = @"1pic_btn03_on";
            break;
            
        case UnlockingService:
            imageName = @"1pic_btn04_on";
            break;
            
        case LostAndFound:
            imageName = @"1pic_btn05_on";
            break;
            
        case OneKeyMove:
            imageName = @"1pic_btn06_on";
            break;
            
        case ResidencePermit:
            imageName = @"1pic_btn07_on";
            break;
            
        case CensusRegisterManagement:
            imageName = @"1pic_btn08_on";
            break;
            
        case Add:
            imageName = @"1pic_btn_addon";
            break;
            
        default:
            break;
    }
    return imageName;
}

/**
 *  查询我的车辆
 */
- (void)getEbicycleInfo
{
    NSString * urlMethod = [NSString stringWithFormat:@"%@getLostReport.do",urlstring];
    NSDictionary * paramDict = @{@"loginId":[[NSUserDefaults standardUserDefaults]objectForKey:@"phonenumber"],@"bid":[[NSUserDefaults standardUserDefaults]objectForKey:@"bid"]};
    
    [K_AppDelegate GenerateHTTPRequestWithUrl:urlMethod withParam: paramDict success:
     ^(AFHTTPRequestOperation *operation, id responseObject) {
         NSDictionary * responseDict = responseObject;
         
         NSNumber * flag = [responseDict objectForKey:@"flag"];
         if (flag.longValue == 0 )
         {
            
             NSString *pro;
             NSString *cit;
             NSString *psb;
             
             NSString *province=[responseDict objectForKey:@"province"];
             if([province isKindOfClass:[NSString class]]){
                 pro=[province stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
             }
             else{
                 pro=@"";
             }
             NSString *city=[responseDict objectForKey:@"city"];
             if([city isKindOfClass:[NSString class]]){
                 cit=[city stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
             }
             else{
                 cit=@"";
             }
             NSString *psbname=[responseDict objectForKey:@"psbName"];
             if([psbname isKindOfClass:[NSString class]]){
                 psb=[psbname stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
             }
             else{
                 psb=@"";
             }
             psbtext=[NSString stringWithFormat:@"%@%@%@",pro,cit,psb];
             time=[responseDict objectForKey:@"lastUpdateTime"];
             eportStatus=[responseDict objectForKey:@"reportStatus"];
             //设置报警备案按钮可用
             [[self getButtonByType:AlarmRecord] setUserInteractionEnabled:YES];
             [[self getButtonByType:AlarmRecord] setImage:[UIImage imageNamed:[self getButtonImageByButtonType:AlarmRecord]] forState:UIControlStateNormal];
         }
         else
         {
             [[self getButtonByType:AlarmRecord] setUserInteractionEnabled:NO];
             [[self getButtonByType:AlarmRecord] setImage:[UIImage imageNamed:@"1pic_btn01_off"] forState:UIControlStateNormal];
         }
         
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"查询防护状态失败！");
         [[self getButtonByType:AlarmRecord] setUserInteractionEnabled:NO];
         [[self getButtonByType:AlarmRecord] setImage:[UIImage imageNamed:@"1pic_btn01_off"] forState:UIControlStateNormal];
     }];
}


/**
 *  获取按钮
 *
 *  @param _type 按钮类型
 *
 *  @return 按钮对象
 */
- (UIButton *) getButtonByType:( ButtonType ) _type
{
    UIView  *  tmpView = nil;
    NSArray * subViews = nil;
    
    if (_type > Add || _type < AlarmRecord)
    {
        return nil;
    }
    
    subViews = self.view.subviews;
    for (int loop = 0; loop < subViews.count; loop++ )
    {
        tmpView = subViews[loop];
        if ( _type == tmpView.tag )
        {
            return (UIButton*)tmpView;
        }
    }
    return nil;
}

@end
